package dao.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import dao.TitleDao;
import po.emp.Empoyee;
import po.emp.Title;

public class TitleDaoImpl implements TitleDao{

	private List<Title> title;
	
	public TitleDaoImpl() {
		title = new ArrayList<Title>();
	}
	
	@Override
	public boolean insert(Title t) {
		if(title.contains(t)) {
			return false;
		}
		return title.add(t);
	}

	@Override
	public boolean update(Title t) {
		int index = title.indexOf(t);
		if(index == -1) {
			return false;
		}else {
			title.set(index, t);
			return true;
		}
	}

	@Override
	public boolean delete(int id) {
		Iterator<Title> it = title.iterator();
		while(it.hasNext()) {
			Title t = it.next();
			if(t.getTitleId() == id) {
				it.remove();
			}
		}
		return false;
	}

	@Override
	public Title selectById(int id) {
		for (Title e : title) {
			if(e.getTitleId() == id) {
				return e;
			}
		}
		return null;
	}

	@Override
	public List<Title> selectAll() {
		List<Title> copy = new ArrayList<>(title);
		return copy;
	}

}
